unit ufTask_Sheduler_PropertyFrequency;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
  Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ComCtrls, Vcl.ExtCtrls,
  uTasks, System.Actions, Vcl.ActnList, Vcl.Buttons, Vcl.Imaging.pngimage;

type
  TfTask_Sheduler_PropertyFrequency = class(TFrame)
    ActionList: TActionList;
    ac_cbOccursEveryValue: TAction;
    ac_cbOccursEveryEndAt: TAction;
    pParams: TPanel;
    Label1: TLabel;
    eOccursOnceAt: TDateTimePicker;
    pHeader: TPanel;
    lExpandList: TLabel;
    Shape2: TShape;
    Panel1: TPanel;
    iUp: TImage;
    iDown: TImage;
    btExpandList: TSpeedButton;
    iExpandDurationList: TImage;
    pExpandBtn: TPanel;
    lExpand: TLabel;
    iExpandExtendedProperty: TImage;
    pAdditional: TPanel;
    cbOccursEveryValue: TCheckBox;
    cbOccursEveryEndAt: TCheckBox;
    eOccursEveryValue: TEdit;
    eOccursEveryType: TComboBox;
    eOccursEveryEndAt: TDateTimePicker;
    cbOccursEveryStartAt: TCheckBox;
    eOccursEveryStartAt: TDateTimePicker;
    ac_cbOccursEveryStartAt: TAction;
    iSmallDown: TImage;
    iSmallUp: TImage;
    procedure ac_cbOccursEveryEndAtUpdate(Sender: TObject);
    procedure btExpandListClick(Sender: TObject);
    procedure lExpandClick(Sender: TObject);
    procedure ac_cbOccursEveryStartAtExecute(Sender: TObject);
    procedure ac_cbOccursEveryValueExecute(Sender: TObject);
  private
    FShedule: TShedule;
    FShowAdditionalBtn: boolean;
    procedure ConfigureUpDownList(Button: TSpeedButton; ListPanel: TWinControl);
    procedure SetupWindow(ShowAdditionalBtn, Expanded: boolean);
  public
    procedure Prepare(AShedule: TShedule; ShowAdditionalBtn: boolean);
    procedure Save;
    procedure Check;
  end;

implementation

{$R *.dfm}

uses
  u1cAgentServerConst, uformDataModule, System.Math;


{ TfTask_Sheduler_PropertyFrequency }

procedure TfTask_Sheduler_PropertyFrequency.ac_cbOccursEveryEndAtUpdate(
  Sender: TObject);
begin
  ac_cbOccursEveryEndAt.Enabled:=cbOccursEveryValue.Checked;
  eOccursEveryEndAt.Enabled:=cbOccursEveryEndAt.Checked;
end;

procedure TfTask_Sheduler_PropertyFrequency.ac_cbOccursEveryStartAtExecute(Sender: TObject);
begin
  cbOccursEveryStartAt.Enabled:= cbOccursEveryValue.Checked;
  eOccursEveryStartAt.Enabled:= cbOccursEveryStartAt.Enabled and cbOccursEveryStartAt.Checked;
end;

procedure TfTask_Sheduler_PropertyFrequency.ac_cbOccursEveryValueExecute(Sender: TObject);
begin
  ac_cbOccursEveryValue.Enabled:= True;

  eOccursEveryValue.Enabled:= cbOccursEveryValue.Checked;
  eOccursEveryType.Enabled:= cbOccursEveryValue.Checked;
end;

procedure TfTask_Sheduler_PropertyFrequency.btExpandListClick(Sender: TObject);
begin
  if Tag = 0 then begin
    Tag:= Height;
    Height:= pHeader.Top + pHeader.Height;
  end else begin
    Height:= Tag;
    Tag:= 0;
  end;

  ConfigureUpDownList(btExpandList, pParams);
end;

procedure TfTask_Sheduler_PropertyFrequency.Check;
begin
  if cbOccursEveryValue.Checked then
    if not((eOccursEveryValue.Text<>'') and (StrToInt(eOccursEveryValue.Text)>0)) then begin
      eOccursEveryValue.SetFocus;
      raise Exception.Create(sSRV_38);
    end;
end;

procedure TfTask_Sheduler_PropertyFrequency.ConfigureUpDownList(Button: TSpeedButton; ListPanel: TWinControl);
begin
  Button.Caption:= '';
  if Tag = 0 then begin
    Button.Glyph.Assign( iUp.Picture );
    Button.Hint:= sServerForm_23;
  end else begin
    Button.Glyph.Assign( iDown.Picture );
    Button.Hint:= sServerForm_24;
  end;
end;

procedure TfTask_Sheduler_PropertyFrequency.lExpandClick(Sender: TObject);
begin
  SetupWindow(True, not pAdditional.Visible);
end;

procedure TfTask_Sheduler_PropertyFrequency.Prepare(AShedule: TShedule; ShowAdditionalBtn: boolean);
begin
  FShedule:= AShedule;
  eOccursOnceAt.Time:= AShedule.OccursOnceAt;
  case FShedule.DailyFrequency of
    dfOnce: begin
      cbOccursEveryValue.Checked:= False;
    end;
    dfEvery: begin
      cbOccursEveryValue.Checked:= True;
      eOccursEveryValue.Text:= IntToStr(AShedule.OccursEveryValue);
      eOccursEveryType.ItemIndex:= Integer(AShedule.OccursEveryType);
      cbOccursEveryStartAt.Checked:= AShedule.OccursEveryUseStartAt;
      eOccursEveryStartAt.Time:= AShedule.OccursEveryStartAt;
      cbOccursEveryEndAt.Checked:= AShedule.OccursEveryUseEndAt;
      eOccursEveryEndAt.Time:= AShedule.OccursEveryEndAt;
    end;
  end;

  FShowAdditionalBtn:= ShowAdditionalBtn;
  SetupWindow(FShowAdditionalBtn, not FShowAdditionalBtn);
  ConfigureUpDownList(btExpandList, pParams);
end;

procedure TfTask_Sheduler_PropertyFrequency.Save;
begin
  FShedule.OccursOnceAt:= eOccursOnceAt.Time;
  if not cbOccursEveryValue.Checked then begin
    FShedule.DailyFrequency:= dfOnce;
  end else begin
    FShedule.DailyFrequency:= dfEvery;
    FShedule.OccursEveryStartAt:= eOccursOnceAt.Time;
    FShedule.OccursEveryValue:= StrToInt(eOccursEveryValue.Text);
    FShedule.OccursEveryType:= TOccursEveryType(eOccursEveryType.ItemIndex);
    FShedule.OccursEveryUseStartAt:= cbOccursEveryStartAt.Checked;
    FShedule.OccursEveryStartAt:= eOccursEveryStartAt.Time;
    FShedule.OccursEveryUseEndAt:= cbOccursEveryEndAt.Checked;
    FShedule.OccursEveryEndAt:= eOccursEveryEndAt.Time;
  end;
end;

procedure TfTask_Sheduler_PropertyFrequency.SetupWindow(ShowAdditionalBtn, Expanded: boolean);
begin
  pExpandBtn.Visible:= ShowAdditionalBtn;
  pAdditional.Visible:= Expanded;

  if Expanded then begin
    lExpand.Caption:= sServerForm_32;
    iExpandExtendedProperty.Picture.Assign(iSmallUp.Picture);
  end else begin
    lExpand.Caption:= sServerForm_33;
    iExpandExtendedProperty.Picture.Assign(iSmallDown.Picture);
  end;
  iExpandExtendedProperty.Left:= lExpand.Left + lExpand.Width + 2;

  Height:= pHeader.Top + pHeader.Height + pParams.Height +
    IfThen(pExpandBtn.Visible, pExpandBtn.Height, 0) +
    IfThen(pAdditional.Visible, pAdditional.Height, 0);
  Realign();
end;

end.
